<template>
  <div>
    <base-list ref="warehouseOperationPoint"
               :condition="condition"
               :datas.sync="storageSet"
               :pageSearch="pageSearch">
      <el-form :inline="true"
               :model="condition"
               class="search-form"
               slot="search"
               label-width="80px">
        <el-row>
          <el-col :span="5" class="search-width4">
            <el-form-item label="所属单位"
                          prop="orgId">
              <auto-search-org v-model="form.orgId"
                               @select="v=>{form.orgId=v;form.deptId='';form.deptName='';}"
                               @selectName="v=>form.orgName=v"
                               :disabled="true"
                               :name="form.orgName"></auto-search-org>
            </el-form-item>
          </el-col>
          <el-col :span="5" class="search-width4">
            <el-form-item label="仓库">
              <el-select v-model="condition['search_EQ_warehouse.code']"
                         placeholder="请选择"
                         clearable>
                <el-option v-for="(item,index) in warehouseList"
                           :key="index"
                           :label="item.name"
                           :value="item.code"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5" class="search-width4">
            <el-form-item label="库房">
              <el-select v-model="condition['search_EQ_roomCode']"
                         placeholder="请选择"
                         clearable>
                <el-option v-for="(item,index) in storageSet"
                           :key="index"
                           :label="item.roomName"
                           :value="item.roomCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-button type="primary"
                       icon="el-icon-search"
                       @click="loadData"
                       class="ml-80">查 询</el-button>
          </el-col>
        </el-row>
        <el-row style="border-bottom: 1px #ddd dashed;margin-bottom: 20px;">
        </el-row>
        <el-row>
          <el-col :span="6" style="padding-right: 10px;">
            <el-row>
              <el-table ref="storageSet"
                        :data="storageSet"
                        border
                        highlight-current-row
                        @current-change="handleCurrentChange">
                <el-table-column label="序号"
                                 fixed="left"
                                 type="index"
                                 width="50"></el-table-column>
                <el-table-column prop="orgName"
                                 label="所属单位"
                                 width="220"></el-table-column>
                <el-table-column prop="warehouseName"
                                 label="仓库"
                                 width="120"></el-table-column>
                <el-table-column prop="roomName"
                                 label="库房"
                                 width="120"></el-table-column>
                <el-table-column prop="jobPointCount"
                                 label="作业点数"
                                 width="80"></el-table-column>
              </el-table>
            </el-row>
          </el-col>
          <el-col :span="18">
            <el-row style="margin-bottom: 10px;">
              <el-button plain
                         icon="el-icon-delete"
                         type="info"
                         @click="handleRemove(multipleSelection)">删除</el-button>
              <el-button
                         icon="el-icon-updata"
                         type="primary"
                         @click="handleDialogFormOpen(true,multipleSelection)">编辑</el-button>
              <el-button
                         icon="el-icon-plus"
                         type="primary"
                         @click="handleDialogFormOpen(false)">
                新增
              </el-button>
            </el-row>
            <el-row>
              <el-table ref="warehouseOperationPoint"
                        :data="warehouseOperationPoint"
                        border
                        @selection-change="handleSelectionChange">
                <el-table-column type="selection"
                                 width="55"></el-table-column>
                <el-table-column fixed="left"
                                 label="序号"
                                 type="index"
                                 width="50"></el-table-column>
                <el-table-column prop="name"
                                 label="作业点名称"
                                 width="120"></el-table-column>
                <el-table-column prop="code"
                                 label="作业点编号"
                                 width="120"></el-table-column>
                <!-- <el-table-column prop="jobTypes"
                                 label="作业点类型"
                                 width="120"></el-table-column> -->
                <el-table-column prop="startSpan"
                                 label="开始跨数"
                                 width="120"></el-table-column>
                <el-table-column prop="endSpan"
                                 label="结束跨数"
                                 width="120"></el-table-column>
                <el-table-column prop="startAula"
                                 label="开始间数"
                                 width="120"></el-table-column>
                <el-table-column prop="endAula"
                                 label="结束间数"
                                 width="120"></el-table-column>
                <el-table-column prop="remark"
                                 label="备注"
                                 width="120"></el-table-column>
                <el-table-column fixed="right"
                                 align="center"
                                 label="操作"
                                 
                                 width="300">
                  <template slot-scope="scope">
                    <el-button type="text"
                               size="small"
                               @click="handleMechanics(scope.row)">
                      关联机械
                    </el-button>
                    <el-button type="text"
                               size="small"
                               @click="handleTerminal(scope.row)">关联终端
                    </el-button>
                    <el-button type="text"
                               size="small"
                               @click="handleWorkType(scope.row)">关联作业类型
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-row>
          </el-col>
        </el-row>
      </el-form>
    </base-list>
    <warehouse-operation-point-form ref="warehouseOperationPointForm"
                                    @load="loadData"></warehouse-operation-point-form>
    <mechanics ref="mechanics"
               @mload="loadData"></mechanics>
    <terminal ref="terminal"
              @tload="loadData"></terminal>
    <work-type ref="workType"
               @wload="loadData"></work-type>
    <div style="text-align:center">
      <el-button plain
                 type="info"
                 @click="handleRouterPush({path: '/goods/storehouse',name: '仓库管理'})">返回</el-button>
    </div>
  </div>
</template>

<script>
import { getWarehouseList } from '@/api/adminWarehouse/adminWarehouse' //所属仓库来源于仓库设置
import { getStorageSetList } from '@/api/storageSet/storageSet' //仓库作业点设置左边表格  所属库房来源于库房设置
import {
  getWarehousePointList,
  deleteWarehousePoint
} from '@/api/warehouseOperationPoint/warehouseOperationPoint' //仓库作业点设置右边表格
import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
import autoSearchDic from '@/components/autocomplete/autoSearchDic'
import autoSearchVehicleNo from '@/components/autocomplete/autoSearchVehicleNo'
import warehouseOperationPointForm from './warehouseOperationPointForm'
import mechanics from './mechanicsForm'
import terminal from './terminalForm'
import workType from './workTypeForm'

export default {
  name: 'warehouseOperationPoint',
  components: {
    autoSearchOrg,
    autoSearchVehicleNo,
    autoSearchDic,
    warehouseOperationPointForm,
    mechanics,
    terminal,
    workType
  },
  data() {
    return {
      form: this.clearForm(),
      condition: {}, // 查询条件
      warehouseOperationPoint: [], // table数组
      pageSearch: getStorageSetList, // 分页查询api接口
      currentRow: null,
      warehouseList:[],
      storageSet: [],
      warehouseName: '',
      roomName: '',
      multipleSelection: []
    }
  },
  mounted() {
    this.$nextTick(function(){
      this.loadData()
    })
    getWarehouseList().then(res => {
      this.warehouseList = res.data.aaData
    })
  },
  methods: {
    clearForm() {
      return {
        jobPoint: [],
        orgId: '',
        orgId:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.id,
        orgName:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.name
      }
    },
    loadData() {
      this.$refs.warehouseOperationPoint.loadData()
    },
    handleDialogFormOpen(bool, item) {
      if (this.currentRow == null) {
        this.$message({ message: '请选择一条数据', type: 'error' })
      } else {
        if (bool) {
          if (item == '') {
            this.$message({ message: '请至少选择一条数据', type: 'error' })
          } else {
            this.$refs.warehouseOperationPointForm.selectRow(item,this.currentRow)
            this.$refs.warehouseOperationPointForm.form = {
              orgId: item[0].orgId,
              orgName: item[0].orgName,
              warehouseCode: item[0].warehouseCode,
              warehouseName: item[0].warehouseName,
              roomCode: item[0].roomCode,
              roomName: item[0].roomName,
              jobPoints: item
            }
            this.$refs.warehouseOperationPointForm.isEdit = true
            this.$refs.warehouseOperationPointForm.dialogForm = true
          }
        } else {
          this.$refs.warehouseOperationPointForm.dialogForm = true
        }
      }
    },
    handleRemove(item) {
      if (item == '') {
        this.$message({
          type: 'error',
          message: `请至少选择一条数据`
        })
      } else {
        this.$confirm(`是否删除该数据?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            for (let i = 0; i < item.length; i++) {
              deleteWarehousePoint(item[i].id).then(res => {
                this.$message({
                  type: 'success',
                  message: `删除成功!`
                })
                this.loadData()
              })
            }
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })
      }
    },
    handleCurrentChange(row) {
      this.currentRow = row
      this.$refs.warehouseOperationPointForm.rectRow(row)
      getWarehousePointList({ 'search_EQ_warehouseRoom.id': row.id }).then(
        res => {
          this.warehouseOperationPoint = res.data.aaData
        }
      )
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleMechanics(val) {
      this.$refs.mechanics.rectMechanics(val)
      this.$refs.mechanics.dialogForm = true
    },
    handleTerminal(val) {
      this.$refs.terminal.dialogForm = true
      this.$nextTick(() => {
        this.$refs.terminal.rectTerminal(val)
        this.$refs.terminal.loadData()
      })
    },
    handleWorkType(val) {
      this.$refs.workType.rectWorkType(val)
      this.$refs.workType.dialogForm = true
    },
    handleRouterPush(val) {
      this.$store.commit('ADD_CACHED_VIEWS', val)
      this.$router.push(val.path)
    }
  }
}
</script>

<style scoped>
</style>
